package com.my.service.imp;

import com.my.dao.PersonDao;
import com.my.dao.UserDao;
import com.my.pojo.Person;
import com.my.pojo.User;
import com.my.service.IPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PersonServiceImpl implements IPersonService {
    private PersonDao personDao;

    private UserDao userDao;

    @Autowired
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public Person showPerson(Integer userId) {
        User user = userDao.getOne(userId);
        if(user == null) {
            return null;
        } else {
            Person person = new Person();
            person.setSex('男');
            personDao.save(person);
            user.setPerson(person);
            userDao.save(user);

            return person;
        }
    }

    @Override
    public Integer updatePerson(Person person, Integer userId) {
        User user = userDao.getOne(userId);
        if(user == null) {
            return -2;
        } else {
            person.setPersonId(user.getPerson().getPersonId());
            return personDao.save(person) == null ? -1 : 0;
        }
    }
}
